package com.gzy.xuexiziliao.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gzy.xuexiziliao.common.result.PageResult;
import com.gzy.xuexiziliao.mapper.InformationCategoriesMapper;
import com.gzy.xuexiziliao.pojo.entity.Categories;
import com.gzy.xuexiziliao.service.InformationCategoriesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class InformationCategoriesServiceImpl implements InformationCategoriesService {
    @Autowired
    private InformationCategoriesMapper informationCategoriesMapper;
    @Override
    public void save(Categories categories) {
        categories.setCreateTime(LocalDateTime.now());
        categories.setUpdateTime(LocalDateTime.now());

        categories.setStatus(0);
        informationCategoriesMapper.save(categories);

    }

    @Override
    public void delete(List<Integer> ids) {

         informationCategoriesMapper.delete(ids);

    }

    @Override
    public void update(Categories categories) {
        categories.setUpdateTime(LocalDateTime.now());


        informationCategoriesMapper.update(categories);
    }

    @Override
    public Categories getById(Integer id) {
        Categories categories = informationCategoriesMapper.getById(id);
        return categories;
    }

    @Override
    public PageResult page(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {
        //1.设置分页参数
        PageHelper.startPage(page,pageSize);
        //2.执行查询
        List<Categories> categoriesList =informationCategoriesMapper.list(name, gender, begin, end);
        Page<Categories> p=(Page<Categories>) categoriesList;

        //3.封装对象
        PageResult pageBean=new PageResult(p.getTotal(),p.getResult());
        return pageBean;
    }

    @Override
    public void startOrCategories(Integer status, Integer id) {
            Categories categories = Categories.builder()
                    .categoryId(id).status(status)
                    .createTime(LocalDateTime.now())
                    .updateTime(LocalDateTime.now())
                    .build();
            informationCategoriesMapper.update(categories);
    }
}
